{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Pass the Pillow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #math #simulation"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #数学 #模拟"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: passThePillow"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #递枕头"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p><code>n</code> 个人站成一排，按从 <code>1</code> 到 <code>n</code> 编号。</p>\n",
    "\n",
    "<p>最初，排在队首的第一个人拿着一个枕头。每秒钟，拿着枕头的人会将枕头传递给队伍中的下一个人。一旦枕头到达队首或队尾，传递方向就会改变，队伍会继续沿相反方向传递枕头。</p>\n",
    "\n",
    "<ul>\n",
    "\t<li>例如，当枕头到达第 <code>n</code> 个人时，TA 会将枕头传递给第 <code>n - 1</code> 个人，然后传递给第 <code>n - 2</code> 个人，依此类推。</li>\n",
    "</ul>\n",
    "\n",
    "<p>给你两个正整数 <code>n</code> 和 <code>time</code> ，返回 <code>time</code> 秒后拿着枕头的人的编号。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 4, time = 5\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>队伍中枕头的传递情况为：1 -&gt; 2 -&gt; 3 -&gt; 4 -&gt; 3 -&gt; 2 。\n",
    "5 秒后，枕头传递到第 2 个人手中。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre>\n",
    "<strong>输入：</strong>n = 3, time = 2\n",
    "<strong>输出：</strong>3\n",
    "<strong>解释：</strong>队伍中枕头的传递情况为：1 -&gt; 2 -&gt; 3 。\n",
    "2 秒后，枕头传递到第 3 个人手中。\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>2 &lt;= n &lt;= 1000</code></li>\n",
    "\t<li><code>1 &lt;= time &lt;= 1000</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [pass-the-pillow](https://leetcode.cn/problems/pass-the-pillow/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [pass-the-pillow](https://leetcode.cn/problems/pass-the-pillow/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['4\\n5', '3\\n2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        # 奇次\n",
    "        if time // (n - 1)  % 2 == 1:\n",
    "            return n - time % (n - 1)\n",
    "        # 偶次\n",
    "        else:\n",
    "            return time % (n - 1) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        div, mod = divmod(time, n - 1)\n",
    "        if div % 2 == 0:\n",
    "            return 1 + mod\n",
    "        else:\n",
    "            return n - mod"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        pos=0\n",
    "        delta=1\n",
    "        for i in range(time):\n",
    "            if pos==0:\n",
    "                delta=1\n",
    "            elif pos==n-1:\n",
    "                delta=-1\n",
    "            pos+=delta\n",
    "        return pos+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time%=2*n-2\n",
    "        if time<=n-1:\n",
    "            return time+1\n",
    "        else:\n",
    "            return 2*n-time-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "       a=time//(n-1)\n",
    "       if a%2==0:\n",
    "           return 1+time%(n-1)\n",
    "       if a%2==1:\n",
    "           return n-time%(n-1)            \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        dirction = int(time/(n-1))\n",
    "        num = time%(n-1)\n",
    "        if dirction%2 == 0:\n",
    "            return 1+num\n",
    "        else:\n",
    "            return n-num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        flag = -1\n",
    "        res = 1\n",
    "        while time:\n",
    "            if res == 1 or res == n:\n",
    "                flag = -1 * flag\n",
    "            res += flag\n",
    "            time -= 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        flag = time // (n-1)\n",
    "        if flag % 2:\n",
    "            return n - (time % (n-1))\n",
    "        else:\n",
    "            return time % (n-1) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        m = time % (2 * n - 2) + 1\n",
    "        if m <= n:\n",
    "            return m\n",
    "        else:\n",
    "            return 2 * n - m\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a=n*2-2\n",
    "        key=time%a\n",
    "        if (key<n):\n",
    "            return key+1\n",
    "        else:\n",
    "            return a-key+1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "\n",
    "    #    return (time%(2*n - 2)) <= n - 1? n: n - (time%(2*n - 2) - n + 1)\n",
    "       s = time % (2*n - 2)\n",
    "       if s <= n - 1:\n",
    "\n",
    "          return s + 1\n",
    "       else:\n",
    "          return n - (s - n + 1)\n",
    "\n",
    "\n",
    "     "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "# 如果 time<n\\textit{time} < ntime<n，枕头没有传递到队尾，传递到 time+1\\textit{time} + 1time+1。\n",
    "# 如果 time≥n\\textit{time} \\ge ntime≥n，枕头已经传递过队尾，传递到 n−(time−(n−1))=n×2−time−1n - (\\textit{time} - (n - 1)) = n \\times 2 - \\textit{time} - 1n−(time−(n−1))=n×2−time−1。\n",
    "        time %= (n-1)*2\n",
    "        return time + 1 if time < n else n*2 - time -1\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        num = time%(n-1)\n",
    "        loop = int(time/(n-1))\n",
    "        #print(num,loop)\n",
    "        if loop%2 == 1:\n",
    "            return  (n - num)\n",
    "        else:\n",
    "            return (num+1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time = time + 1\n",
    "        # if n <= time:\n",
    "        #     remain = time % n\n",
    "        #     marker = time // n\n",
    "\n",
    "        #     print(marker)\n",
    "        #     if marker % 2 != 0:\n",
    "        #         result = n - remain\n",
    "        #         return result\n",
    "\n",
    "        #     return remain + marker\n",
    "\n",
    "        # return time\n",
    "\n",
    "        pos = 1\n",
    "        rev = 1\n",
    "        while time != 1:\n",
    "            if pos == n:\n",
    "                rev = -1\n",
    "            elif pos == 1:\n",
    "                rev = 1\n",
    "\n",
    "            pos += rev\n",
    "            time -= 1\n",
    "\n",
    "        return pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "         k, mod = divmod(time, n - 1)\n",
    "         return n - mod if k & 1 else mod + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        x = time//(n - 1)\n",
    "        if time < n:\n",
    "            i = time + 1\n",
    "        elif x%2 == 0:\n",
    "            i = time - x*(n - 1) + 1\n",
    "        else:\n",
    "            i = n*(x + 1) - time - x\n",
    "        return(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        num=time % (n-1)\n",
    "        turn=time//(n-1)\n",
    "        if turn % 2 == 0:\n",
    "            return num+1\n",
    "        else:\n",
    "            return n-num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        direction=1 #初始传递方向 1表示向右 -1表示向左\n",
    "        currentPerson=1 # 当前拿着枕头人的编号\n",
    "        while time>0:\n",
    "            #如果当前传递方向是向右，且当前人是最后一人，或者当前传递方向是向左，且传递的是第一人，改变传递方向\n",
    "            if (direction==1 and currentPerson==n) or (direction==-1 and currentPerson==1):\n",
    "                direction*=-1\n",
    "            #计算下一个拿着枕头人的编号\n",
    "            if direction==1:\n",
    "                currentPerson+=1\n",
    "            else:\n",
    "                currentPerson-=1\n",
    "            time-=1\n",
    "        return currentPerson\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a=time%(2*n-2)\n",
    "        if a>n-1:\n",
    "            return 2*n-a-1\n",
    "        else:\n",
    "            return a+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "      t = time % (2*n-2)\n",
    "      return n - abs(t+1-n)\n",
    "      "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        state=False\n",
    "        sum=1\n",
    "        for i in range(time):\n",
    "            if state:\n",
    "                sum-=1\n",
    "            else:\n",
    "                sum+=1\n",
    "            if sum==1:\n",
    "                state=not state\n",
    "            if sum==n:\n",
    "                state=not state\n",
    "        return sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if (-1)**(time//(n-1))==-1:\n",
    "            return n-time%(n-1)\n",
    "        return time%(n-1)+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        loop = 2 * (n - 1)\n",
    "        time %= loop\n",
    "        if time > n - 1:\n",
    "            return 1 + loop - time\n",
    "        else:\n",
    "            return 1 + time"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        return n-abs(time%(2*n-2)-n+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        re_time = time % (n - 1) # 传递枕头不够一个队列的时间\n",
    "        count = time // (n - 1) # 传递枕头够一个队列的次数\n",
    "        person_num1 = 1 if count & 1 == 0 else n\n",
    "        person_num2 = re_time + 1 if count & 1 == 0 else n - re_time\n",
    "        return person_num1 if re_time == 0 else person_num2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time = time % (2*(n-1))\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        else:\n",
    "            return n * 2 - time - 1\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a, b = divmod(time, n-1)\n",
    "        if a % 2 == 0:\n",
    "            return 1+b\n",
    "        else:\n",
    "            return n-b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans=1\n",
    "        turn=time//(n-1)+1\n",
    "        remain=time%(n-1)\n",
    "        if time/(n-1)==0:\n",
    "            return n\n",
    "        if turn%2==1:\n",
    "            ans+=remain\n",
    "        else:\n",
    "            ans=n-remain\n",
    "        return ans\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans=time%(n-1)\n",
    "        return ans+1 if time//(n-1)%2==0 else n-ans\n",
    "        x=1\n",
    "        for _ in range(time):\n",
    "            ans+=x\n",
    "            if ans==n or ans==1:\n",
    "                x=-x\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        return min(2*n-1-(time%(2*(n-1))),time%(2*(n-1))+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        k, t = divmod(time, n - 1)\n",
    "        return n - t if k % 2 else 1 + t"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        # if n <= time:\n",
    "        #     remain = time % n\n",
    "        #     marker = time // n\n",
    "\n",
    "        #     print(marker)\n",
    "        #     if marker % 2 != 0:\n",
    "        #         result = n - remain\n",
    "        #         return result\n",
    "\n",
    "        #     return remain + marker\n",
    "\n",
    "        # return time\n",
    "\n",
    "        pos = 1\n",
    "        rev = 1\n",
    "        while time != 0:\n",
    "            if pos == n:\n",
    "                rev = -1\n",
    "            elif pos == 1:\n",
    "                rev = 1\n",
    "\n",
    "            pos += rev\n",
    "            time -= 1\n",
    "\n",
    "        return pos"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        return time % (n - 1) + 1 if time // (n - 1) % 2 == 0 else n - time % (n - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time = time % (2*n-2)\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        else:\n",
    "            return 2*n - time - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        hui = 2*(n-1)\n",
    "        time %= hui\n",
    "        if time<=n-1: return (1+time)\n",
    "        else:\n",
    "            time-=(n-1)\n",
    "            return (n-time)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        flag=1\n",
    "        index=0\n",
    "        while time!=0:\n",
    "            index+=flag\n",
    "            time-=1\n",
    "            if  index==0 or index==n-1:\n",
    "                flag=-flag\n",
    "        return index+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a,b = time//(n-1),time%(n-1)\n",
    "        if a%2:\n",
    "            return n-b\n",
    "\n",
    "        else:\n",
    "            return b+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        # #  问题分为time次 从1开始\n",
    "        # #  result = 1 \n",
    "        # result = 1 \n",
    "        # op = 1 \n",
    "        # for i in range(1, time + 1):\n",
    "        #     if i % n  == 0 or (result != 1 and i == 1):\n",
    "        #         op = -op \n",
    "        #     result += op \n",
    "        # return result \n",
    "\n",
    "        position = 1\n",
    "        direction = 1 \n",
    "        for i in range(1, time + 1):\n",
    "            position += direction\n",
    "            if position == 1 or position == n:\n",
    "                direction = -direction\n",
    "        return position"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans = k =1\n",
    "        for i in range(time):\n",
    "            ans += k\n",
    "            if ans == 1 or ans == n:\n",
    "                k *= -1\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if time%(n-1) == 0:\n",
    "            return 1 if time/(n-1)%2 == 0 else n\n",
    "        else:\n",
    "            return 1 + time%(n-1) if int(time/(n-1))%2 == 0 else n - time%(n-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow_1(self, n: int, time: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        m, d = time // (n-1), time % (n-1)\n",
    "        if m % 2 == 0:\n",
    "            return d+1\n",
    "        else:\n",
    "            return n-d\n",
    "\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        # time = time%(n-1)*2\n",
    "        a = time // (n-1)\n",
    "        b = time % (n-1)\n",
    "        print(a)\n",
    "        if a%2==1:\n",
    "            return n-b\n",
    "        else:\n",
    "            return b+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans=1\n",
    "        flag=-1\n",
    "        for i in range(time):\n",
    "            if ans==n:\n",
    "                flag*=-1\n",
    "            if ans==1:\n",
    "                flag*=-1\n",
    "\n",
    "            ans+=flag\n",
    "        return ans\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time=time%(2*(n-1))\n",
    "        if time<n:\n",
    "            return time+1\n",
    "        else:\n",
    "            return 2*(n-1)-time+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        yu = time % (2*(n-1))\n",
    "        new = [i for i in range(1, n+1)] + [i for i in range(1, n)][::-1]\n",
    "        return new[yu]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        direction = 1\n",
    "        pillow = 1\n",
    "        for t in range(0,time):\n",
    "            print(pillow)\n",
    "            if pillow == n and direction == 1:\n",
    "                direction = 0\n",
    "                pillow = n-1\n",
    "            elif pillow == 1 and direction == 0:\n",
    "                direction = 1\n",
    "                pillow = 2\n",
    "            elif pillow != n and direction == 1:\n",
    "                pillow += 1\n",
    "            elif pillow != 1 and direction == 0:\n",
    "                pillow -= 1\n",
    "        return pillow"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if (time // (n - 1)) % 2 == 0:\n",
    "            return 1 + time % (n - 1)\n",
    "\n",
    "        else:\n",
    "            return n - time % (n - 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time = time % (2*(n-1))\n",
    "        if time <= n-1:\n",
    "            return time + 1\n",
    "        else:\n",
    "            return n - (time - n + 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time%=2*(n-1)\n",
    "        return time+1 if time<n else 2*n-time-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= 2 * (n - 1)\n",
    "        if time > n - 1:\n",
    "            return n - (time - (n - 1))\n",
    "        return time + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        k,count=time%(n-1),time//(n-1)\n",
    "        if count%2==0:\n",
    "            person=1+k\n",
    "        else:\n",
    "            person=n-k\n",
    "        return person\n",
    "        \n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if n > time:\n",
    "            return time + 1\n",
    "        time %= (2 * n - 2)\n",
    "\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        return 2 * n - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        m, d = time // (n-1), time % (n-1)\n",
    "        if m % 2 == 0:\n",
    "            return d+1\n",
    "        else:\n",
    "            return n-d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n",
    "\n",
    "# 作者：力扣官方题解\n",
    "# 链接：https://leetcode.cn/problems/pass-the-pillow/solutions/2451117/di-zhen-tou-by-leetcode-solution-kl5e/\n",
    "# 来源：力扣（LeetCode）\n",
    "# 著作权归作者所有。商业转载请联系作者获得授权，非商业转载请注明出处。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow_1(self, n: int, time: int) -> int:\n",
    "        if n == 1:\n",
    "            return 1\n",
    "        m, d = time // (n-1), time % (n-1)\n",
    "        if m % 2 == 0:\n",
    "            return d+1\n",
    "        else:\n",
    "            return n-d\n",
    "\n",
    "    def passThePillow_2(self, n: int, time: int) -> int:\n",
    "        time %= (n - 1) * 2\n",
    "        return time + 1 if time < n else n * 2 - time - 1\n",
    "\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        return min(time%(2*(n-1))+1,2*(n-1)-time%(2*(n-1))+1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans=1\n",
    "        x=1\n",
    "        for _ in range(time):\n",
    "            ans+=x\n",
    "            if ans==n or ans==1:\n",
    "                x=-x\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        i = time%(2*n-2)\n",
    "        return i+1 if i<n else n*2-i-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a,b = time//(n-1),time%(n-1)\n",
    "        if a%2:\n",
    "            return n-b\n",
    "\n",
    "        else:\n",
    "            return b+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if time%((n-1)*2)<=n-1:\n",
    "            return time%((n-1)*2)+1\n",
    "        else:\n",
    "            return n-(time%((n-1)*2)-n+1)\n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        #解题思路\n",
    "        '''每次枕头需要经过2 * (n - 1)轮才会重新回到1的手中，于是可以将time对这个值取模。接下来对于取模后的time，若其位于前半区，也就是time < n - 1时，会按照正序传递，此时对应的答案就是time + 1，反之当time位于后半区时，会按逆序传递，此时对应的答案就是2 * n - 1 - time。'''\n",
    "        if (time // (n - 1)) % 2:\n",
    "            return n - (time % (n - 1))\n",
    "        else:\n",
    "            return (time % (n - 1)) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time = time % (2 * n - 2)\n",
    "        if time <= n - 1:\n",
    "            return time + 1\n",
    "        else:\n",
    "            return 2 * n - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (2 * n - 2)\n",
    "\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        return 2 * n - time - 1\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a,r=divmod(time,n-1)\n",
    "        if a%2==0:\n",
    "            return r+1\n",
    "        else:\n",
    "            return n-r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        a = n-1\n",
    "        time = time % (a*2)\n",
    "        \n",
    "        if time >a:\n",
    "            return 2*n-time-1\n",
    "        else:\n",
    "            return time+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        return (time % (n-1))+1 if (time // (n-1))%2==0 else n-(time % (n-1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        ans = k = 1\n",
    "        for _ in range(time):\n",
    "            ans += k\n",
    "            if ans == 1 or ans == n:\n",
    "                k *= -1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (n-1)*2\n",
    "        return time+1 if time<n else n*2-time-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        #解题思路\n",
    "        ''''''\n",
    "        if (time // (n - 1)) % 2:\n",
    "            return n - (time % (n - 1))\n",
    "        else:\n",
    "            return (time % (n - 1)) + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        time %= (2 * n - 2)\n",
    "        return time + 1 if time < n else 2 * n - time - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        c = time % (2 * n - 2)\n",
    "        ans = c if c < n else 2 * n - c - 2\n",
    "        return ans + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        yu = time % (2 * n - 2)\n",
    "        if yu < n:\n",
    "            return yu + 1\n",
    "        else:\n",
    "            return 2 * n - yu - 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        nums = list(range(1,n+1)) + list(range(n-1,1,-1))\n",
    "        yu = time%(2*n-2)\n",
    "        return nums[yu]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def passThePillow(self, n: int, time: int) -> int:\n",
    "        if time < n:\n",
    "            return time + 1\n",
    "        if time // (n-1) % 2 == 1:\n",
    "            return n - time % (n-1)\n",
    "        else:\n",
    "            return time % (n-1) + 1"
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
